package com.blensmile.testvido.utils;

import android.text.TextUtils;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class LogUtils {

    public static final boolean isDebug = Configs.DEBUG;
    private static final int STACK_SIZE = 4000;

    private static LogUtils instance;

    private LogUtils(){}

    public static LogUtils getInstance(){
        if(instance == null){
            synchronized (LogUtils.class){
                if(instance == null){
                    instance = new LogUtils();
                }
            }
        }
        return instance;
    }

    public static void d(String tag,String message){
        Log.d(tag,message);
    }

    public static void e(String tag,String message){
        if(isDebug){
            Log.e(tag, message);
        }
    }

    public static void w(String tag,String msg){
        if(isDebug){
            Log.e(tag,msg);
        }
    }

    public static void v(String tag,String message){
        if(isDebug){
        }
    }
    public static void i(String tag,String message){
        if(isDebug){
            Log.i(tag,message);
        }
    }
    public static void b(String message){
        d("blen",message);
    }

    public static void b(Object... message){
        StringBuilder builder = new StringBuilder();
        for(Object item:message){
            builder.append(item).append(" ");
        }
        d("blen",builder.toString());
    }

    public static void l(String tag,Object... content){
        StringBuilder builder = new StringBuilder();
        for(Object item : content){
            builder.append(item).append(" -> ");
        }
        i(tag,builder.toString());
    }

    public static void json(String tag, String json){
        String message = json;
        if (!TextUtils.isEmpty(json) && !(message.length() > STACK_SIZE)) {
            try {
                json = json.trim();
                if (json.startsWith("{")) {
                    JSONObject jsonObject = new JSONObject(json);
                    message = jsonObject.toString(2);
                }else if (json.startsWith("[")) {
                    JSONArray jsonArray = new JSONArray(json);
                    message = jsonArray.toString(2);
                }
            } catch (JSONException e) {}
        }
        printBulk(tag,message);
    }

    public static String getArrayString(Object[] o){
        StringBuilder builder = new StringBuilder();
        for(Object item:o){
            builder.append(item.toString()).append("\n");
        }
        return builder.toString();
    }

    public static void printBulk(String tag,String log){
        if(isDebug) {
            printBulk(tag, log, STACK_SIZE);
        }
    }

    private static void printBulk(String tag,String log,int showCount){
        int index = 0;
        int length = log.length();
        while (index <= length){
            Log.d(tag+index,log.substring(index,(index+=showCount)<length?index:length));
        }
    }
}
